﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Enums;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;
using VisionCloud.Domain.UnitOfWork;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsFileStatusRepository : BaseRepository<MsFileStatus>, IMsFileStatusRepository
    {
        public IUnitOfWork UnitOfWork { get; set; }

        /// <summary>
        /// 新增或更新文件处理状态
        /// </summary>
        /// <param name="unzipTaskDataEntityList"></param>
        /// <param name="fileStatus"></param>
        /// <returns></returns>
        public async Task UpdateFileStatusAsync(List<UnzipTaskDataEntity> unzipTaskDataEntityList, FileStatusEnum fileStatus)
        {
            if (unzipTaskDataEntityList.IsEmpty())
                return;

            var fileArr = unzipTaskDataEntityList.Select(p => p.FileKey);
            var existingMsFileStatusList = await this.WhereAsync(p => fileArr.Contains(p.FileKey));
            foreach (var unzipTaskDataEntity in unzipTaskDataEntityList)
            {
                if (!unzipTaskDataEntity.IsHaveError)
                {
                    var existingMsFileStatus = existingMsFileStatusList.FirstOrDefault(p => p.FileKey == unzipTaskDataEntity.FileKey);
                    if (existingMsFileStatus != null)
                    {
                        existingMsFileStatus.FileStatus = fileStatus.ToString();
                        existingMsFileStatus.LastUpdatedBy = "UnzipTaskDataService";
                        existingMsFileStatus.LastUpdatedDate = DateTime.Now;
                        existingMsFileStatus.UsbKeyNO = unzipTaskDataEntity.UsbKeyNO;
                        existingMsFileStatus.SyncStatus = "10";
                        this.Update(existingMsFileStatus, false);
                    }
                    else
                    {
                        var msFileStatus = new MsFileStatus
                        {
                            FileKey = unzipTaskDataEntity.FileKey,
                            FileStatus = fileStatus.ToString(),
                            IsDeleted = false,
                            CreatedDate = DateTime.Now,
                            CreatedBy = "UnzipTaskDataService",
                            LastUpdatedBy = "UnzipTaskDataService",
                            LastUpdatedDate = DateTime.Now,
                            UsbKeyNO = unzipTaskDataEntity.UsbKeyNO,
                            SyncStatus = "10"
                        };

                        this.Add(msFileStatus, false);
                    }
                }
            }

            await UnitOfWork.SaveChangesAsync();
        }

        /// <summary>
        /// 新增或更新文件处理状态
        /// </summary>
        /// <param name="unzipTaskDataEntity"></param>
        /// <param name="fileStatus"></param>
        /// <returns></returns>
        public async Task UpdateFileStatusAsync(UnzipTaskDataEntity unzipTaskDataEntity, FileStatusEnum fileStatus)
        {
            unzipTaskDataEntity.IsHaveError = false;
            await UpdateFileStatusAsync(new List<UnzipTaskDataEntity> { unzipTaskDataEntity }, fileStatus);
        }
    }
}
